

package com.mt.repair.controller.repairManagement;

import org.springframework.security.access.prepost.PreAuthorize;
import com.mt.common.core.annotation.ApiPageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.mt.common.core.web.base.PageDTO;
import com.mt.common.core.web.base.PageResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



import com.mt.repair.dto.repairManagement.DeviceEditDto;
import com.mt.repair.entity.repairManagement.Device;
import com.mt.repair.service.repairManagement.DeviceService;
																		

@Api(tags = "设备")
@RestController
@RequestMapping("/api/repair/repairManagement/Device")
@CrossOrigin(allowCredentials = "true")
public class DeviceController {
private static Logger logger = LoggerFactory.getLogger(DeviceController.class);



																																																									@Autowired private DeviceService deviceService;
	
	/**
	* 根据分页参数查询设备集合
	*
	* @param pageDTO 分页条件
	*/
	@PreAuthorize("hasAuthority('repair:repairManagement:Device:view')")
	@ApiOperation("分页查询设备")
	@ApiPageParam
	@PostMapping("/findDevices")
	public PageResultDTO findDevices(@RequestBody PageDTO pageDTO){
		return this.deviceService.findDevices(pageDTO);
	}

	/**
	* 根据ID查询指定的设备
	*
	* @param deviceId Id
	*/
	@PreAuthorize("hasAuthority('repair:repairManagement:Device:view')")
	@ApiOperation("根据id查询设备")
	@ApiPageParam
	@PostMapping("/findDevice")
	public Device findDevice(@RequestParam Long deviceId){
		return this.deviceService.findDevice(deviceId);
	}

	/**
	* 根据ID查询指定的设备(包含外键名称)
	*
	* @param deviceId Id
	*/
	@PreAuthorize("hasAuthority('repair:repairManagement:Device:view')")
	@ApiOperation("根据ID查询指定的设备(包含外键名称)")
	@PostMapping("/findDeviceForView")
	public Device findDeviceForView(@RequestParam Long deviceId){
		return this.deviceService.findDeviceWithForeignName(deviceId);
	}

	/**
	* 根据ID查询指定的设备(包含设备和外键名称)
	*
	* @param deviceId Id
	*/
	@PreAuthorize("hasAuthority('repair:repairManagement:Device:view')")
	@ApiOperation("根据ID查询指定的设备(包含设备和外键名称)")
	@PostMapping("/findDeviceForEdit")
	public DeviceEditDto findDeviceForEdit(@RequestParam Long deviceId){
				DeviceEditDto deviceEditDto = new DeviceEditDto();
		deviceEditDto.setDevice(this.deviceService.findDeviceWithForeignName(deviceId));

		this.prepareDeviceEditDto(deviceEditDto);

		return deviceEditDto;
	}

	/**
	* 根据ID查询指定的设备(只提取ID 和 Name)
	*
	* @param deviceId Id
	*/
	@PreAuthorize("hasAuthority('repair:repairManagement:Device:view')")
	@ApiOperation("根据ID查询指定的设备(只提取ID 和 Name)")
	@PostMapping("/findDevicesWithIdNameById")
	public Device findDevicesWithIdNameById(@RequestParam Long deviceId){
	return this.deviceService.findDevicesWithIdNameById(deviceId);
	}

	/**
	* 根据名称查询设备集合(只提取ID 和 Name)
	*
	* @param deviceName 名称
	*/
	@PreAuthorize("hasAuthority('repair:repairManagement:Device:view')")
	@ApiOperation("根据名称查询设备集合(只提取ID 和 Name)")
	@PostMapping("/findDevicesWithIdNameByName")
	public List<Device> findDevicesWithIdNameByName(@RequestParam String deviceName){
	return this.deviceService.findDevicesWithIdNameByName(deviceName);
	}


	/**
	* 创建新的设备
	*
	*/
	@PreAuthorize("hasAuthority('repair:repairManagement:Device:add')")
	@ApiOperation("创建新的设备")
	@PostMapping("/createDevice")
	public DeviceEditDto createDevice(){
				DeviceEditDto deviceEditDto = new DeviceEditDto();
		deviceEditDto.setDevice(new Device());

		this.prepareDeviceEditDto(deviceEditDto);
		return deviceEditDto;
	}

	private void prepareDeviceEditDto(DeviceEditDto deviceEditDto){
																																																								}

	/**
	* 新增保存设备
	*
	* @param device 实体对象
	*/
	@PreAuthorize("hasAuthority('repair:repairManagement:Device:add')")
	@ApiOperation("新增保存设备")
	@PostMapping("/saveDevice")
	public Device saveDevice(@RequestBody Device device) throws IOException {
		return this.deviceService.saveDevice(device);
	}

	/**
	* 修改保存设备
	*
	* @param device 实体对象
	*/
	@PreAuthorize("hasAuthority('repair:repairManagement:Device:update')")
	@ApiOperation("修改保存设备")
	@PostMapping("/updateDevice")
	public Device updateDevice(@RequestBody Device device){
		return this.deviceService.updateDevice(device);
	}

	/**
	* 根据ID删除设备
	*
	* @param deviceId ID
	*/
	@PreAuthorize("hasAuthority('repair:repairManagement:Device:remove')")
	@ApiOperation("根据ID删除设备")
	@PostMapping("/deleteDevice")
	public void deleteDevice(@RequestParam Long deviceId){
		this.deviceService.deleteDevice(deviceId);
	}

	/**
	 * 根据管理用户查询设备集合
	 *
	 * @param userId 用户ID
	 */
	//@PreAuthorize("hasAuthority('repair:repairManagement:Device:view')")
	@ApiOperation("根据管理用户查询设备集合")
	@PostMapping("/findDevicesByUser")
	public List<Device> findDevicesByUser(@RequestParam Long userId){
		return this.deviceService.findDevicesByUser(userId);
	}
	/**
	 * 根据管理用户查询设备集合
	 *
	 * @param deviceCode s设备编号
	 */
	//@PreAuthorize("hasAuthority('repair:repairManagement:Device:view')")
	@ApiOperation("根据设备编号查询")
	@PostMapping("/findDeviceByCode")
	public Device findDeviceByCode(@RequestParam String deviceCode){
		return this.deviceService.findDeviceByCode(deviceCode);
	}
}

